package cn.czh0123.redis.service.impl;

import cn.czh0123.domain.WallpaperRecord;
import cn.czh0123.enums.WallpaperOperateType;
import cn.czh0123.mapper.WallpaperRecordMapper;
import cn.czh0123.redis.service.IWallpaperOperateRedisService;
import cn.czh0123.redis.service.IWallpaperStatisticsRedisService;
import cn.czh0123.redis.vo.WallpaperStatisticsVO;
import cn.czh0123.utils.RedisKeys;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;

@Slf4j
@Service
public class WallpaperStatisticsRedisServiceImpl implements IWallpaperStatisticsRedisService {

    @Resource
    private IWallpaperOperateRedisService wallpaperOperateRedisService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private WallpaperRecordMapper wallpaperRecordMapper;

    @Override
    public WallpaperStatisticsVO getWallpaperStatistics(Long wallpaperId) {
        String key = RedisKeys.WALLPAPER_STATISTICS.join(wallpaperId.toString());

        if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(key))) {
            String value = stringRedisTemplate.opsForValue().get(key);
            return JSON.parseObject(value, WallpaperStatisticsVO.class);
        }

        // 查询数据库和缓存中的操作记录
        List<WallpaperRecord> wallpaperRecords = wallpaperRecordMapper.selectList(Wrappers.<WallpaperRecord>lambdaQuery().eq(WallpaperRecord::getWallpaperId, wallpaperId));
        wallpaperRecords.addAll(wallpaperOperateRedisService.getWallpaperOperateRecordByWallpaperId(wallpaperId));

        Map<WallpaperOperateType, Long> operationCounts = wallpaperRecords.stream()
                .collect(groupingBy(record -> WallpaperOperateType.fromCode(record.getOperateType()), counting()));

        WallpaperStatisticsVO statisticsVO = new WallpaperStatisticsVO();
        statisticsVO.setWallpaperId(wallpaperId);
        statisticsVO.setViewNum(operationCounts.getOrDefault(WallpaperOperateType.VIEW, 0L));
        statisticsVO.setCommentNum(operationCounts.getOrDefault(WallpaperOperateType.COMMENT, 0L));
        statisticsVO.setFavorNum(operationCounts.getOrDefault(WallpaperOperateType.COLLECT, 0L));
        statisticsVO.setLikeNum(operationCounts.getOrDefault(WallpaperOperateType.LIKE, 0L));
        statisticsVO.setDownloadNum(operationCounts.getOrDefault(WallpaperOperateType.DOWNLOAD, 0L));

        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(statisticsVO));
        return statisticsVO;
    }

    @Override
    public void setWallpaperStatistics(WallpaperStatisticsVO wallpaperStatisticsVO) {
        String key = RedisKeys.WALLPAPER_STATISTICS.join(wallpaperStatisticsVO.getWallpaperId().toString());
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(wallpaperStatisticsVO));
    }

}
